﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;

[assembly: TagPrefix("Azad.BCL.CustomGrid", "CustomGrid")]
namespace Azad.BCL.CustomGrid
{
    public class CustomLinkBoundControlField : DataControlField
    {

        public string DataField
        {
            get
            {
                object value = ViewState["DataField"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["DataField"] = value;

                OnFieldChanged();
            }
        }

        public string URL
        {
            get
            {
                object value = ViewState["URL"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["URL"] = value;

                OnFieldChanged();
            }
        }
        public string Text
        {
            get
            {
                object value = ViewState["Text"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["Text"] = value;

                OnFieldChanged();
            }
        }

        #region " Methods "


        protected override DataControlField CreateField()
        {
            return new CustomLinkBoundControlField();
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            base.ExtractValuesFromCell(dictionary, cell, rowState, includeReadOnly);
            string value = null;

            if (cell.Controls.Count > 0)
            {
                Control control = cell.Controls[0];
                //If the control is null, throw an error
                if (control == null)
                    throw new InvalidOperationException("The control cannot be extracted");
                value = ((TextBox)control).Text;
            }

            //If the key exists, update the value
            if (dictionary.Contains(this.DataField))
                dictionary[this.DataField] = value;
            //Add a new entry to the dictionary
            else
                dictionary.Add(this.DataField, value);
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            Control control = null;

            if (cellType == DataControlCellType.DataCell)
            {
                if (this.IsReadMode(rowState))
                {
                    control = cell;
                }
            }

            //Capture the data binding event when data binding
            if (control != null && this.Visible)
                control.DataBinding += new EventHandler(control_DataBinding);
        }

        protected bool IsReadMode(DataControlRowState rowState)
        {
            return (rowState == DataControlRowState.Normal || rowState == DataControlRowState.Alternate || rowState == DataControlRowState.Selected);
        }

        #endregion

        #region " Event Handlers "

        void control_DataBinding(object sender, EventArgs e)
        {
            if (sender is TableCell)
            {
                TableCell cell = sender as TableCell;
                object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
                string _text = DataBinder.GetPropertyValue(dataItem, this.DataField, null);
                string _DisplayText = DataBinder.GetPropertyValue(dataItem, this.Text, null);
                LinkButton lnk = new LinkButton();
                lnk.Text = _DisplayText;//this.Text;

                lnk.OnClientClick = "javascript:window.location ='" + Control.ResolveUrl(this.URL) + _text + "'; return false;";
                lnk.ID = "a" + cell.Text;
                cell.Controls.Add(lnk);
            }

        }

        #endregion
    }
}
